﻿@{
    ViewBag.Title = "Search";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<div id="PresentationBox">
    <div>
        <table class="DataPresentationtable" id="searchTable">
            <tr>
                <td><center><img src="/Content/images/loading.gif" />loading data...</center></td>
            </tr>
        </table>
    </div>
</div>

<script type="text/javascript">
    function createTitleObject(val) {
        var title = [];

        title.Title = "<a href='/Title/Get/" + val.Title.TitleId + "'>" + val.Title.BookTitle + "</a>";

        title.Loanables = val.Loanables.length;
        title.EditionNumber = val.Title.EditionNumber;
        title.EditionYear = val.Title.EditionYear;
        title.Publisher = val.Title.Publisher.Name;

        return title;
    }

    function createAuthorObject(val) {
        var author = [];

        author.Name = "<a href='/Author/Get/" + val.Author.Aid + "'>" + val.Author.Name + "</a>";
        author.Titles = val.Titles.length;

        return author;
    }

    function createTopicObject(val) {
    	var topic = [];
    	topic.TopicName = "<a href='/Topic/Get/" + val.Topic.TopicId + "'>" + val.Topic.TopicName + "</a>";
    	topic.Titles = val.Titles.length;
    	return topic;
    }

    var searchTerm = '@ViewBag.searchTerm';
    var searchOption = '@ViewBag.searchOption';
    var dataSet = [];
    $(document).ready(function () {
        $.getJSON("/api/search/?searchTerm=" + encodeURIComponent(searchTerm) + "&searchOption=" + encodeURIComponent(searchOption), function (data) {
            var columns = [];
            if (searchOption == "Title" || searchOption == "ISBN-10" || searchOption == "ISBN-13") {
                //process the title data
                $.each(data.Titles, function (key, val) {
                    console.log(val);
                    var obj = createTitleObject(val);
                    dataSet.push(obj);
                });

                columns = [
                    { "data": "Title", "title": "Title", "sWidth": "60%" },
                    { "data": "Loanables", "title": "Loanables", "sWidth": "10%" },
                    { "data": "EditionNumber", "title": "Edition", "sWidth": "10%" },
                    { "data": "EditionYear", "title": "Year", "sWidth": "10%" },
                    { "data": "Publisher", "title": "Publisher", "sWidth": "10%" }
                ];
            } else if (searchOption == "Author") {
                $.each(data.Authors, function (key, val) {
                    console.log(val);
                    var obj = createAuthorObject(val);
                    dataSet.push(obj);
                });

                columns = [
                    { "data": "Name", "title": "Name" },
					{ "data": "Titles", "title": "Number of titles" }
                ];
            } else if (searchOption == "Topic") {
            	$.each(data.Topics, function (key, val) {
            		console.log(val);
            		var obj = createTopicObject(val);
            		dataSet.push(obj);
            	});

            	columns = [
                    { "data": "TopicName", "title": "Topic name" },
					{ "data": "Titles", "title": "Number of titles" }
            	];
            }

            $('#searchTable').DataTable({
                "bFilter": false,
                "data": dataSet,
                "fnDrawCallback": function () {
                    if (Math.ceil((this.fnSettings().fnRecordsDisplay()) / this.fnSettings()._iDisplayLength) > 1) {
                        $('#searchTable_paginate').css("display", "block");
                    } else {
                        $('#searchTable_paginate').css("display", "none");
                    }
                },
                "columns": columns
            });

            //remove ugly DataTable css
            $('#searchTable').removeClass("dataTable");
        });
    });
</script>